//
//  ViewControllerTool.m
//  LoL
//
//  Created by lanou3g on 16/3/8.
//  Copyright © 2016年 Wicky. All rights reserved.
//

#import "ViewControllerTool.h"
#import <UIImageView+WebCache.h>
#import "ScrVModel.h"
@implementation ViewControllerTool
-(void)makeScrollVWithDic:(NSDictionary *)dic
                  forSCRV:(UIScrollView *)scrV
               WithHeight:(CGFloat)height
{
    scrV.pagingEnabled = YES;
    scrV.showsHorizontalScrollIndicator = NO;
    scrV.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
    NSArray *arr = dic[@"data"];
    for (int i = 0; i < arr.count + 1; i++)
    {
        NSMutableString * urlStr = nil;
        ScrVModel * model = [ScrVModel new];
        if (i == arr.count)
        {
            [model setValuesForKeysWithDictionary:arr[0]];
        }
        else
        {
            [model setValuesForKeysWithDictionary:arr[i]];
        }
        urlStr = model.cover.mutableCopy;
        UIImageView * imgV = [[UIImageView alloc] initWithFrame:CGRectMake(kScreenWidth * i, 0, kScreenWidth, height)];
        imgV.contentMode = UIViewContentModeScaleToFill;
        [imgV sd_setImageWithURL:[NSURL URLWithString:urlStr]];
        [scrV addSubview:imgV];
    }
    scrV.contentSize = CGSizeMake(arr.count * kScreenWidth,height);
}
-(void)getDataWithUrlStr:(NSString *)urlStr
            SuccessBlock:(SuccessBlock)success
{
    NSURLSession * session = [NSURLSession sharedSession];
    NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlStr]];
    NSURLSessionTask * task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        if (data) {
            NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
            dispatch_async(dispatch_get_main_queue(), ^{
                if (success) {
                    success(dic);
                }
            });
        }
    }];
    [task resume];
}

- (NSString*)weekdayStringFromDate:(NSString *)dateStr {
    NSDateFormatter * formatter = [NSDateFormatter new];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate * date = [formatter dateFromString:dateStr];
    NSArray *weekdays = [NSArray arrayWithObjects: [NSNull null], @"Sun.", @"Mon.", @"Tues.", @"Wed.", @"Thur.", @"Fri.", @"Sat.", nil];
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:@"Asia/Shanghai"];
    [calendar setTimeZone: timeZone];
    NSCalendarUnit calendarUnit = NSCalendarUnitWeekday;
    NSDateComponents *theComponents = [calendar components:calendarUnit fromDate:date];
    return [weekdays objectAtIndex:theComponents.weekday];
}
@end
